import 'package:flutter/material.dart';

import 'list_tile_skeleton.dart';
import 'skeleton_animation.dart';
import 'skeleton_decoration.dart';

/// Create by kyluzoi
/// Date: 2021-08-17
///
/// Description:

abstract class BaseSkeletonWidget extends StatefulWidget {
  final SkeletonStyle style;

  BaseSkeletonWidget({
    Key? key,
    this.style: const SkeletonStyle.origin(),
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => buildState();

  buildState();
}

abstract class BaseSkeletonState<Page extends BaseSkeletonWidget> extends State<Page>
    with SingleTickerProviderStateMixin {
  late SkeletonAnimation _skeletonAnimation;

  @override
  void initState() {
    super.initState();
    if (widget.style.isAnimation) {
      _skeletonAnimation = SkeletonAnimation(this);
    }
  }

  @override
  void dispose() {
    if (widget.style.isAnimation) {
      _skeletonAnimation.dispose();
    }
    super.dispose();
  }

  Color get _backgroundColor {
    if (widget.style.backgroundColor != null) {
      return widget.style.backgroundColor!;
    }
    if (widget.style.theme == SkeletonTheme.Dark) {
      return darkBackgroundColor;
    }
    return lightBackgroundColor;
  }

  EdgeInsetsGeometry get _padding {
    if (widget.style.padding != null) {
      return widget.style.padding!;
    }
    return EdgeInsets.all(16.0);
  }

  SkeletonDecoration createSkeletonDecoration(bool isCircle) {
    List<Color> colors = [];
    if (widget.style.colors != null && (widget.style.colors?.length ?? 0) > 0) {
      colors = widget.style.colors!;
    } else {
      colors =
          widget.style.theme != SkeletonTheme.Dark ? lightColors : darkColors;
    }

    return SkeletonDecoration(
      animation: _skeletonAnimation,
      isCircle: isCircle,
      isAnimation: widget.style.isAnimation,
      colors: colors,
    );
  }

  @override
  Widget build(BuildContext context) {
    return widget.style.isAnimation
        ? AnimatedBuilder(
            animation: _skeletonAnimation.animation,
            builder: (context, child) {
              return renderContainer();
            },
          )
        : renderContainer();
  }

  Widget renderContainer();
}
